
var page=0;
//新增收货地址
$('.add_btn').click(function () {
    window.location.href="add_address2.html"
})

//获取所有收获地址
$(function () {
    doAjax();
})

function doAjax() {
    $.ajax({
        url:'/user/getUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                var addData = data.datas.addressLest;
                if(addData.length==0){
                    $('#addressBox').empty();
                    $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#addressBox'));
                }
                for(var i=0;i<addData.length;i++){
                    var ifDefault = '';
                    var ifDefaultValue = '';
                    if(addData[i].ifDefault){
                        ifDefault='../img/selected.png';
                        ifDefaultValue = '默认地址';
                    }else{
                        ifDefault='../img/select.png';
                        ifDefaultValue = '设为默认';
                    }
                    $('<div class="address_box">' +
                        '<div class="user_info">' +
                        '<span class="name">'+addData[i].name+'</span>' +
                        '<span class="phone">'+addData[i].phone+'</span></div>' +
                        '<div class="address_detail" provinceId="'+addData[i].provinceId+'" cityId="'+addData[i].cityId+'" countyId="'+addData[i].countyId+'">' +
                        '<span class="three">'+addData[i].province+addData[i].city+addData[i].county+'</span><span class="bigDetail">'+addData[i].detail+'</span></div>' +
                        '<div class="address_set" id="'+addData[i].id+'">' +
                        '<span class="default"><img src="'+ifDefault+'" alt="默认地址"><i>'+ifDefaultValue+'</i></span>' +
                        '<span class="delete"><img src="../img/delete.jpg" alt="删除">删除</span>' +
                        '<span class="edit"><img src="../img/edit.jpg" alt="编辑">编辑</span>' +
                        '</div></div>').appendTo($('#addressBox'))
                }
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
        }
    })
}

//设为默认地址
$('body').on('click','.default',function () {
    var addressId = $(this).parent().attr('id');
    var setThis = $(this);
    if(setThis.children().attr('src')=='../img/selected.png'){

    }else{
        $.ajax({
            url:'/user/userAddressToBeDefault.json',
            dataType:'json',
            data:{
                token:getItem('token'),
                addressId:addressId,
            },
            type:'post',
            success:function (data) {
                if(data.code==1){
                    setThis.children().attr('src','../img/selected.png')
                    setThis.parent().parent().siblings().find('.default').children().attr('src','../img/select.png')
                }
                if(data.code==-1){
                    window.location.href="login.html"
                }
                if(data.code==0){
                    mui.toast(data.msg);
                    return;
                }
            }
        })
    }
})

//编辑
$('body').on('click','.edit',function () {
    var addressId = $(this).parent().attr('id');
    var name = $(this).parent().siblings('.user_info').children('.name').html();
    var phone = $(this).parent().siblings('.user_info').children('.phone').html();
    var detail = $(this).parent().siblings('.address_detail').children('.three').html();
    var detailBox = $(this).parent().siblings('.address_detail').children('.bigDetail').html();
    var proId = $(this).parent().siblings('.address_detail').attr('provinceId');
    var cityId = $(this).parent().siblings('.address_detail').attr('cityId');
    var countyId = $(this).parent().siblings('.address_detail').attr('countyId');
    window.location.href=encodeURI(encodeURI("add_address2.html?addressId="+addressId+"&name="+name+"&phone="+phone+"&detail="+detail+"&detailBox="+detailBox+"&proId="+proId+"&cityId="+cityId+"&countyId="+countyId));
})

//删除
$('body').on('click','.delete',function () {
    var addressId = $(this).parent().attr('id');
    var setThis = $(this);
    $.ajax({
        url:'/user/deleteUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            addressId:addressId,
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('删除成功',function () {
                    setThis.parent().parent().remove();
                });
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})